package com.huzz.controller;

import cn.dev33.satoken.annotation.SaCheckSafe;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.huzz.entity.LoginModel;
import com.huzz.model.RegisterModel;
import com.huzz.service.SaTokenService;
import com.huzz.util.body.JsonResult;
import com.huzz.util.body.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name 名称
 * @Description 描述
 * @Author 童年的纸飞机
 * @Since 2023-04-2023/4/25 15:48
 */
@Api(tags = "系统安全认证模块 API")
@RestController
@RequestMapping("/api/sa-token")
public class SaTokenController {

    @Autowired
    private SaTokenService saTokenService;

    @ApiOperation(value = "登录")
    @PostMapping(path = "/login")
    public JsonResult<Object> login(@RequestBody LoginModel loginModel) {
        return ResultTool.response(saTokenService.login(loginModel));
    }

    @ApiOperation(value = "二级认证")
    @PostMapping("openSafe")
    public JsonResult<String> openSafe(@RequestParam String service, @RequestParam String secondPassword) {
        return ResultTool.response(saTokenService.openSafe(service, secondPassword));
    }

    @SaIgnore
    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public JsonResult<String> register(@RequestBody RegisterModel registerModel) {
        return ResultTool.response(saTokenService.register(registerModel));
    }

    @SaIgnore
    @ApiOperation(value = "查询登录状态")
    @GetMapping("/isLogin")
    public JsonResult<String> isLogin() {
        return ResultTool.response(saTokenService.isLogin());
    }

    @ApiOperation(value = "注销登录")
    @GetMapping("/logout")
    public JsonResult<String> logout() {
        saTokenService.logout();
        return ResultTool.success("退出成功");
    }

    @ApiOperation(value = "获取当前会话的Token信息")
    @GetMapping("/getTokenInfo")
    public JsonResult<SaTokenInfo> getTokenInfo() {
        return ResultTool.success(saTokenService.getTokenInfo());
    }

    @ApiOperation(value = "获取当前账号所有权限")
    @GetMapping("/getPermissionList")
    public JsonResult<List<String>> getPermissionList() {
        return ResultTool.success(saTokenService.getPermissionList());
    }

    @ApiOperation(value = "获取当前账号所有角色")
    @GetMapping("/getRoleList")
    public JsonResult<List<String>> getRoleList() {
        return ResultTool.success(saTokenService.getRoleList());
    }

    @SaCheckSafe(value = "sa_token_manager")
    @ApiOperation(value = "强制注销|封号|踢人下线")
    @GetMapping("/coerceLogout")
    public JsonResult<String> coerceLogout(@RequestParam String value,
                                           @ApiParam(defaultValue = "86400")
                                           @RequestParam Long time,
                                           @ApiParam(
                                                   name = "type",
                                                   allowableValues = "logout_loginId,logout_loginId_PC,logout_loginId_phone,logout_token," +
                                                           "disable_loginId_time," +
                                                           "kickout_loginId,kickout_loginId_PC,kickout_loginId_phone,kickout_token"
                                           )
                                           @RequestParam String type) {
        return ResultTool.success(saTokenService.coerceLogout(value, time, type));
    }

    @ApiOperation(value = "获取封号剩余时间")
    @GetMapping("/getDisableTime")
    public JsonResult<String> getDisableTime(@RequestParam Integer loginId) {
        return ResultTool.success(saTokenService.getDisableTime(loginId));
    }

    @ApiOperation(value = "获取当前用户角色/权限/菜单列表")
    @GetMapping("/getCurrentUserInfo")
    public JsonResult<JSONObject> getCurrentUserInfo() {
        return ResultTool.success(saTokenService.getCurrentUserInfo());
    }

    @ApiOperation(value = "测试")
    @GetMapping("/getTokenValueByLoginId")
    public JsonResult getTokenValueByLoginId(@RequestParam Integer loginId) {
        return ResultTool.success(StpUtil.searchTokenValue(null, 0, -1, true));
    }

    @ApiOperation(value = "解封账号")
    @GetMapping("/untieDisable")
    public JsonResult<String> untieDisable(@RequestParam Integer loginId) {
        saTokenService.untieDisable(loginId);
        return ResultTool.success("解封成功");
    }



}
